package com.frogs.ucenter.v1.request;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.exception.RequestIllegalException;

/**
 * 根据手机号码重置密码请求参数类
 *
 * @author lijian
 * @version 2014年4月26日 - 上午10:39:18
 */
public class UserResetPasswordByPhoneRequest extends Request {
	
	private int userId;
	
	private String phone;
	
	private String key;
	
	private String passwd;
	
	private String confirmPasswd;
	
	private String ip;

	@Override
	public void parse(HttpServletRequest request) {
		HttpRequestInfo requestInfo = new HttpRequestInfo(request);
		
		this.userId = requestInfo.getIntParameter(ApiUrlParamEnum.userId.getParamName(), 0);
		this.key = requestInfo.getParameter(ApiUrlParamEnum.key.getParamName(), "");
		this.passwd = requestInfo.getParameter(ApiUrlParamEnum.password.getParamName(), "");
		this.confirmPasswd = requestInfo.getParameter(ApiUrlParamEnum.confirmPassword.getParamName(), "");
		this.phone = requestInfo.getParameter(ApiUrlParamEnum.phone.getParamName(), "");
		this.ip = requestInfo.getParameter(ApiUrlParamEnum.ip.getParamName(), requestInfo.getUserIp());
		if (userId < 1) {
			throw new RequestIllegalException("用户id有误");
		}
		if (!StringUtil.isMobilePhone(phone)) {
			throw new RequestIllegalException("用户手机号码有误");
		}
		if (StringUtil.isEmpty(key)) {
			throw new RequestIllegalException("key不能为空");
		}
		if (StringUtil.isEmpty(passwd) || StringUtil.isEmpty(confirmPasswd)) {
			throw new RequestIllegalException("密码不能为空");
		}
		if (passwd.length() < 6 || passwd.length() > 20) {
			throw new RequestIllegalException("密码的长度在6-20位");
		}
		if (confirmPasswd.length() < 6 || confirmPasswd.length() > 20) {
			throw new RequestIllegalException("密码的长度在6-20位");
		}
		if (!Pattern.matches("[a-zA-Z0-9`~!@#$%^&*()+=_|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]+$", passwd)
				|| !Pattern.matches("[a-zA-Z0-9`~!@#$%^&*()+=_|{}':;',//[//]\\.<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]+$", confirmPasswd)) {
			throw new RequestIllegalException("密码是由字符，数字和字母组成");
		}
		if (!passwd.equals(confirmPasswd)) {
			throw new RequestIllegalException("两次输入的密码不一致");
		}
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public String getConfirmPasswd() {
		return confirmPasswd;
	}

	public void setConfirmPasswd(String confirmPasswd) {
		this.confirmPasswd = confirmPasswd;
	}
	
	public String getIp() {
		return ip;
	}
	
	public void setIp(String ip) {
		this.ip = ip;
	}

}
